package de.eeb;

import de.unikoeln.sharedbucks.R;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import de.eeb.common.SBException;
import de.eeb.common.SBWSController;



public class MainViewActivity extends Activity 
{
	
	public static final String PREFS_NAME = "SharedBucksPrefs";
	public static SBWSController SBController;


	/** Called when the activity is first created. */
    @Override
    
    
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);  
        getWindow().setBackgroundDrawableResource(R.drawable.money);
        
        
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
				.permitAll().build();
		StrictMode.setThreadPolicy(policy);

		SBController = new SBWSController();
		
		try {
			
			
//			SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
//			String number = settings.getString("OwnNumber", "na");
			
			String number = "+4917661028489";
			
			SharedPreferences settings = getSharedPreferences(MainViewActivity.PREFS_NAME, 0);
			SharedPreferences.Editor editor = settings.edit();
			editor.putString("OwnNumber", number);
			
			if(!editor.commit()) {
				Log.e(RegisterUserActivity.class.getName(), "Cannot store own number in preferences: " + number);
			}			
			
			

			if ("na".equals(number) || !SBController.isUser(number)) {
				Log.d(RegisterUserActivity.class.getName(), "Don't know own phone number, jumping to register user...");
				Intent in = new Intent(MainViewActivity.this, RegisterUserActivity.class);
				startActivity(in);
			}
		} catch (SBException e) {
			Log.e(MainViewActivity.class.getName(), e.getMessage());
		}

    
	    Button btnActivity = (Button)findViewById(R.id.do_new_payment);
	    
	    btnActivity.setOnClickListener(new View.OnClickListener()
    		{
    			public void onClick(View v) {
    				Intent in = new Intent (MainViewActivity.this, CreateNewPaymentActivity.class);
    				startActivity(in);
    			}
    		}
	    );
    
	    Button btnActivity2 = (Button)findViewById(R.id.do_show_payments);
	    btnActivity2.setOnClickListener(new View.OnClickListener()
    		{
    			public void onClick(View v) {
    				Intent in = new Intent (MainViewActivity.this, OverviewUnfinishedPaymentActivity.class);
    				startActivity(in);
    			}
    		}
	    );
    
	    Button btnActivity3 = (Button)findViewById(R.id.do_finish_payment);
	    btnActivity3.setOnClickListener(new View.OnClickListener()
    		{
    			public void onClick(View v) {
    				Intent in = new Intent (MainViewActivity.this, FinishPaymentActivity.class);
    				startActivity(in);
    			}
    		}
	    );
    
    }
    
}